/** 
 * @Title:监听节点变化，新增or删除 
 * @Desription:监听节点变化，新增or删除
 * @Company:CSN
 * @ClassName:NodePathCacheListener.java
 * @Author:Justic
 * @CreateDate:2015-6-30   
 * @UpdateUser:Justic  
 * @Version:0.1 
 *    
 */

package com.justic.zk.quartz.curator.support;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: NodePathCacheListener
 * @Description: 监听节点变化，新增or删除
 * @author: Justic
 * @date: 2015-6-30
 * 
 */
public class NodePathCacheListener implements PathChildrenCacheListener {

	private static Logger logger = LoggerFactory.getLogger(NodePathCacheListener.class);

	private PathChildrenCache cached;

	/**
	 * @param childrenCache
	 */
	public NodePathCacheListener(PathChildrenCache cached) {
		this.cached = cached;
	}

	/**
	 * 事件捕捉
	 * 
	 * @param client
	 * @param event
	 * @throws Exception
	 * @see org.apache.curator.framework.recipes.cache.PathChildrenCacheListener#childEvent(org.apache.curator.framework.CuratorFramework,
	 *      org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)
	 */
	@Override
	public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {

		switch (event.getType()) {
		case CHILD_ADDED:
			logger.info("CHILD_ADDED: {}", event.getData().getPath());
			break;
		case CHILD_REMOVED:
			logger.info("CHILD_REMOVED: {}", event.getData().getPath());
			break;
		case CHILD_UPDATED:
			logger.info("CHILD_UPDATED: {}", event.getData().getPath());
			break;
		case CONNECTION_RECONNECTED:
			cached.rebuild();
			break;
		case CONNECTION_SUSPENDED:
		case CONNECTION_LOST:
			logger.info("Connection error,waiting...");
			break;
		case INITIALIZED:
			logger.info("Initialized ...");
			break;
		default:
			logger.info("{} : {}", event.getType(), event.getData().getPath());
			break;

		}
	}

}
